
#import "LoginViewController.h"
#import "PrivateViewController.h"

@interface LoginViewController ()<UITextFieldDelegate>
@property (strong, nonatomic) IBOutlet UIView *bgView;
@property (strong, nonatomic) IBOutlet UIView *bgInpu3;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *bgInput3Top;
@property (strong, nonatomic) IBOutlet BaseTextField *input1;
@property (strong, nonatomic) IBOutlet BaseTextField *input2;
@property (strong, nonatomic) IBOutlet BaseTextField *input3;
@property (strong, nonatomic) IBOutlet UIButton *registerBtn;
@property (strong, nonatomic) IBOutlet UIButton *loginBtn;
@property (strong, nonatomic) IBOutlet UIButton *back;
@property (strong, nonatomic) IBOutlet UIImageView *bgImg;

@end

@implementation LoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
}
- (void)initUI{
    self.bgView.layer.masksToBounds = NO;
    self.bgView.layer.shadowColor = [UIColor colorHex:@"#9B9B9B"].CGColor;
    self.bgView.layer.shadowOffset = CGSizeMake(0, 3);
    self.bgView.layer.shadowRadius = 12;
    self.bgView.layer.cornerRadius = 12;
    self.bgView.layer.shadowOpacity = 1;
    
    self.back.hidden = !self.isLogin;
    self.bgInpu3.hidden = self.isLogin;
    self.bgInput3Top.constant = self.isLogin ? -54 : 25;
    NSString *title1 = self.isLogin ? LS(@"没有账号？注册") : LS(@"已有账号，登录");
    [self.registerBtn setTitle:title1 forState:UIControlStateNormal];
    NSString *title2 = self.isLogin ? LS(@"登录") : LS(@"注册");
    [self.loginBtn setTitle:title2 forState:UIControlStateNormal];
}
- (IBAction)actionForBtn:(UIButton *)sender {
    if (sender.tag == 204) {
        return [self backBtnAction];
    }
    if ([sender.currentTitle isEqualToString:LS(@"没有账号？注册")]) {
        LoginViewController *vc = [LoginViewController new];
        vc.isLogin = NO;
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([sender.currentTitle isEqualToString:LS(@"已有账号，登录")]){
        [self backBtnAction];
    }else if ([sender.currentTitle isEqualToString:LS(@"登录")]){
        if ([self judgeInput]) {
            [UserTool login:self.input1.text psw:self.input2.text complete:^(NSDictionary * _Nonnull dict) {
                int code = [dict[@"code"] intValue];
                if (code == 200) {
                    APP.user = dict[@"data"];
                    [self backBtnAction];
                }else{
                    [Toast msg:LS(dict[@"msg"])];
                }
            }];
        }
    }else if ([sender.currentTitle isEqualToString:LS(@"注册")]){
        if ([self judgeInput]){
            [UserTool register:self.input1.text psw:self.input2.text complete:^(NSDictionary * _Nonnull dict) {
                int code = [dict[@"code"] intValue];
                if (code == 200) {
                    LoginViewController *vc   = self.navigationController.viewControllers[self.navigationController.viewControllers.count - 2];
                    vc.input1.text = self.input1.text;
                    vc.input2.text = self.input2.text;
                    [self backBtnAction];
                }else{
                    [Toast msg:LS(dict[@"msg"])];
                }
            }];
        }
    }
}


- (BOOL)judgeInput{
    if (self.input1.text.length <= 0){
        [Toast msg:LS(@"账号不能为空")];
        return NO;
    }else if (self.input2.text.length <= 0){
        [Toast msg:LS(@"密码不能为空")];
        return NO;
    }else if ([self.input1.text isNumberOrAlphabet] == NO){
        [Toast msg:LS(@"账号只能包含数字和字母")];
        return NO;
    }else if ([self.input2.text isNumberOrAlphabet] == NO){
        [Toast msg:LS(@"密码只能包含数字和字母")];
        return NO;
    }
    else{
        return YES;
    }
}

@end
